package vn.lumi.smarthome.ui;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Floor;
import vn.lumi.smarthome.io.database.TBL_Room;
import vn.lumi.smarthome.ulti.LogUltis;
import vn.lumi.smarthome.ulti.Preference;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorTreeAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MenuFragment extends BaseFragment implements
		LoaderCallbacks<Cursor> {
	public static final String TAG = "MenuFragment";

	private ExpandableListView mExpandableListView;
	private TextView mtxtTitle;
	private ImageButton mSetting;
	private MenuAdapter mMenuAdapter;
	private OnFloorRoomClickListener mOnFloorRoomClickListener;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mOnFloorRoomClickListener = (OnFloorRoomClickListener) activity;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_menu, container, false);
		init_view(view);
		init_loadData();
		return view;
	}

	private void init_view(View view) {
		mtxtTitle = (TextView) view.findViewById(R.id.fragment_menu_txt_title);
		mSetting = (ImageButton) view.findViewById(R.id.fragment_menu_setting);
		mSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent i = new Intent(getActivity(), SettingActivity.class);
				getActivity().startActivityForResult(i,
						MainActivity.SETTING_REQUEST);
				;
			}
		});
		mExpandableListView = (ExpandableListView) view
				.findViewById(R.id.fragment_menu_expand_listview);
		mExpandableListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				MenuAdapter menuAdapter = (MenuAdapter) parent
						.getExpandableListAdapter();
				long childId = menuAdapter.getChildId(groupPosition,
						childPosition);
				long groupId = menuAdapter.getGroupId(groupPosition);
				mOnFloorRoomClickListener.onFloorRoomClick((int) groupId,
						(int) childId, menuAdapter.getGroupName(groupPosition),
						menuAdapter.getChildName(groupPosition, childPosition));
				return true;
			}
		});
		mtxtTitle.setText(getString(R.string.txt_title_hello) + " "
				+ Preference.getUID(getActivity()));
	}

	private void init_loadData() {
		getLoaderManager().initLoader(0, null, this);
	}

	/**
	 * Interface for item in menu is clicked
	 * 
	 * @author TUNGDX
	 * 
	 */
	public interface OnFloorRoomClickListener {
		/**
		 * Event when click
		 * 
		 * @param floor_id
		 *            id of floor which is clicked
		 * @param room_id
		 *            id of room which is clicked
		 */
		public void onFloorRoomClick(int floor_id, int room_id,
				String floor_name, String room_name);
	}

	private static class MenuAdapter extends CursorTreeAdapter {
		private Context mContext;

		public MenuAdapter(Cursor cursor, Context context) {
			super(cursor, context);
			mContext = context;
		}

		@Override
		protected void bindChildView(View view, Context context, Cursor cursor,
				boolean isLastChild) {
			TextView textView = (TextView) view
					.findViewById(R.id.item_expandablelistview_item_txt);
			textView.setText(cursor.getString(cursor
					.getColumnIndex(TBL_Room.NAME)));
			view.setBackgroundDrawable(view.getResources().getDrawable(
					R.color.greenLight));
		}

		@Override
		protected void bindGroupView(View view, Context context, Cursor cursor,
				boolean isExpanded) {
			if(getGroupCount() == 1){
				view.setVisibility(View.INVISIBLE);
			}
			else {
				view.setBackgroundDrawable(view.getResources().getDrawable(R.color.greenActionbar));
				TextView textView=(TextView)view.findViewById(R.id.item_expandablelistview_groupview_txt);
				textView.setText(cursor.getString(cursor.getColumnIndex(TBL_Floor.NAME)));
			}
			
		}

		@Override
		protected Cursor getChildrenCursor(Cursor groupCursor) {
			LogUltis.i(TAG, "getChildrenCursor");
			int index = groupCursor.getColumnIndex(TBL_Floor._ID);
			LogUltis.i(TAG, "index=" + index);
			int floor_id = groupCursor.getInt(index);
			LogUltis.i(TAG, "Floor id=" + floor_id);
			String selection = TBL_Room.FLOOR_ID + "=?";
			String[] where = new String[] { floor_id + "" };
			return mContext.getContentResolver().query(
					TBL_Room.CONTENT_URI_ROOM, null, selection, where, null);
		}

		@Override
		protected View newChildView(Context context, Cursor cursor,
				boolean isLastChild, ViewGroup parent) {
			return View.inflate(context, R.layout.item_expandablelistview_item,
					null);
		}

		@Override
		protected View newGroupView(Context context, Cursor cursor,
				boolean isExpanded, ViewGroup parent) {
			
			View view = View.inflate(context,
					R.layout.item_expandablelistview_groupview, null);
			return view;
		}

		@Override
		public long getGroupId(int groupPosition) {
			Cursor cursor = getGroup(groupPosition);
			if (cursor != null) {
				return cursor.getInt(cursor.getColumnIndex(TBL_Floor._ID));
			}
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			Cursor cursor = getChild(groupPosition, childPosition);
			if (cursor != null) {
				return cursor.getInt(cursor.getColumnIndex(TBL_Room._ID));
			}
			return childPosition;
		}

		public String getChildName(int groupPosition, int childPosition) {
			Cursor cursor = getChild(groupPosition, childPosition);
			if (cursor != null) {
				return cursor.getString(cursor.getColumnIndex(TBL_Room.NAME));
			}
			return "";
		}

		public String getGroupName(int groupPosition) {
			Cursor cursor = getGroup(groupPosition);
			if (cursor != null) {
				return cursor.getString(cursor.getColumnIndex(TBL_Floor.NAME));
			}
			return "";
		}

		@Override
		public Cursor getGroup(int groupPosition) {
			Cursor cursor = getCursor();
			if (cursor != null) {
				cursor.moveToPosition(groupPosition);
				return cursor;
			}
			return super.getGroup(groupPosition);
		}

		@Override
		public int getGroupCount() {
			if (getCursor() != null)
				return getCursor().getCount();
			return 0;
		}

		@Override
		public Cursor getChild(int groupPosition, int childPosition) {
			Cursor groupCursor = getGroup(groupPosition);
			Cursor childCursor = null;
			if (groupCursor != null) {
				childCursor = getChildrenCursor(groupCursor);
				childCursor.moveToPosition(childPosition);
				return childCursor;
			}
			return super.getChild(groupPosition, childPosition);
		}

	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
		return new CursorLoader(getActivity(), TBL_Floor.CONTENT_URI_FLOOR,
				null, null, null, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		LogUltis.i(TAG, "Load MenuFragment finish, rows=" + cursor.getCount());
		mMenuAdapter = new MenuAdapter(cursor, getActivity());
		mExpandableListView.setAdapter(mMenuAdapter);

		// Mo rong expandlist
		mExpandableListView.expandGroup(0);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		mMenuAdapter.changeCursor(null);
	}

	@Override
	public void refreshData(int floor_id, int room_id) {

	}
}
